package com.smart.upms.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.core.starter.base.BaseController;
import com.smart.core.starter.model.R;
import com.smart.upms.sys.param.SysZuulRouteParam;
import com.smart.upms.sys.param.SysZuulRouteQueryParam;
import com.smart.upms.sys.result.SysZuulRouteResult;
import com.smart.upms.sys.service.ISysZuulRouteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;

/**
 * <p>
 * 动态路由配置 前端控制器
 * </p>
 *
 * @author guxiaobai
 * @since 2018-12-11
 */
@Api(description = "网关路由管理接口")
@RestController
@RequestMapping("/sys/sys-zuul-route")
@AllArgsConstructor
public class SysZuulRouteController extends BaseController {
    private final ISysZuulRouteService sysZuulRouteService;


    /**
     * 添加路由
     *
     * @param param 路由信息
     * @return success、false
     */
    @ApiOperation(value = "添加路由")
    @PostMapping
    public R<Boolean> add(@Validated @RequestBody SysZuulRouteParam param) {
        return R.success(sysZuulRouteService.save(param));
    }


    /**
     * 修改路由
     *
     * @param param 路由信息
     * @return success/false
     */
    @ApiOperation(value = "修改路由")
    @PutMapping
    public R<Boolean> update(@Validated @RequestBody SysZuulRouteParam param) {
        return R.success(sysZuulRouteService.update(param));
    }

    /**
     * 删除路由
     *
     * @param id ID
     * @return R
     */
    @ApiOperation(value = "删除路由")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.success(sysZuulRouteService.delete(id));
    }


    /**
     * 通过ID查询
     *
     * @param id ID
     * @return 路由信息
     */
    @ApiOperation("通过ID查询")
    @GetMapping("/{id}")
    public R<SysZuulRouteResult> get(@Min(1L) @PathVariable Long id) {
        SysZuulRouteResult result = sysZuulRouteService.get(id);
        return R.success(result);
    }

    /**
     * 分页查询
     *
     * @param param 查询参数
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R<Page<SysZuulRouteResult>> page(SysZuulRouteQueryParam param) {
        Page<SysZuulRouteResult> page = getPage(param);
        return R.success(sysZuulRouteService.listPage(page, param));
    }


    /**
     * 刷新路由
     *
     * @return success/fasle
     */
    @GetMapping("/apply")
    public R<Boolean> apply() {
        return R.success(sysZuulRouteService.apply());
    }


}
